import Cookies from 'js-cookie';
import { defineStore } from 'pinia';

interface AppState {
  sidebar: {
    opened: boolean;
    withoutAnimation: boolean;
    hide: boolean;
  };
  device: string;
  size: string;
}

const useAppStore = defineStore('app', {
  state: (): AppState => ({
    sidebar: {
      opened: Number(Cookies.get('sidebarStatus')) ? !!+Number(Cookies.get('sidebarStatus')) : true,
      withoutAnimation: false,
      hide: false,
    },
    device: 'desktop',
    size: Cookies.get('size') || 'default',
  }),
  actions: {
    toggleSideBar(withoutAnimation?: boolean) {
      if (this.sidebar.hide) {
        return false;
      }
      this.sidebar.opened = !this.sidebar.opened;
      if (withoutAnimation) this.sidebar.withoutAnimation = withoutAnimation;
      if (this.sidebar.opened) {
        Cookies.set('sidebarStatus', '1');
      } else {
        Cookies.set('sidebarStatus', '0');
      }
    },
  },
});

export default useAppStore;
